package servlet;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Notice;
import DAO.NoticeDao;

public class NoticeListAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ActionForward forward=new ActionForward();

		ArrayList <Notice> noticeIdList = new ArrayList <Notice>();
		ArrayList <Notice> noticeList = new ArrayList <Notice>();
		
		NoticeDao noticeDao = new NoticeDao();
		request.setAttribute("checkValue", "checkValue");
		
		 HttpSession session=request.getSession();
			
			
			int studyId = request.getParameter("studyId") != null
					&& request.getParameter("studyId") != "" ? Integer
					.parseInt(request.getParameter("studyId")) : 0;
			int userIdCheck = session.getAttribute("userid") != null
					&& session.getAttribute("userid") != "" ? Integer
					.parseInt((String)session.getAttribute("userid")) : 0;
			if (userIdCheck < 1) {
				session.invalidate();
				Util.ShowError(response, "로그인해주세요");
			}
			if (studyId < 1) {
				Util.ShowError(response, "잘못 된 접근입니다");
			}
			
		noticeIdList = noticeDao.getAllNoticeList(studyId);
		
		for(int i=0; i<noticeIdList.size(); i++)
		{
			Notice notice = noticeDao.getNoticeInfo(noticeIdList.get(i).getNoticeId());
			noticeList.add(notice);
		}
	
		
		request.setAttribute("studyId", studyId);	
		request.setAttribute("noticeList", noticeList);
		
		
		forward.setPath("./study_notice.jsp");
		
		return forward;
	}

}
